﻿namespace Sidvall.WorkItemManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class WorkCondition : global::Sidvall.WorkItemManager.UI.Entities.Core.BusinessBase, global::Sidvall.WorkItemManager.Data.IWorkCondition
    {
        #region Business Members

        #region Codename

        private string _Codename = string.Empty;
        partial void OnBeforeCodenamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeCodenamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterCodenamePropertyChanged();
        public string Codename
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Codename);
                OnBeforeCodenamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Codename);
                OnBeforeCodenamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Codename", eventData.Value, true, true, true);
                    OnAfterCodenamePropertyChanged();
                }
            }
        }

        #endregion
        #region ContextEditor

        private string _ContextEditor = string.Empty;
        partial void OnBeforeContextEditorPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeContextEditorPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterContextEditorPropertyChanged();
        public string ContextEditor
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ContextEditor);
                OnBeforeContextEditorPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ContextEditor);
                OnBeforeContextEditorPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ContextEditor", eventData.Value, true, true, true);
                    OnAfterContextEditorPropertyChanged();
                }
            }
        }

        #endregion
        #region Description

        private string _Description = string.Empty;
        partial void OnBeforeDescriptionPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeDescriptionPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterDescriptionPropertyChanged();
        public string Description
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Description);
                OnBeforeDescriptionPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Description);
                OnBeforeDescriptionPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Description", eventData.Value, true, true, true);
                    OnAfterDescriptionPropertyChanged();
                }
            }
        }

        #endregion
        #region Name

        private string _Name = string.Empty;
        partial void OnBeforeNamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeNamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterNamePropertyChanged();
        public string Name
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Name);
                OnBeforeNamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Name);
                OnBeforeNamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Name", eventData.Value, true, true, true);
                    OnAfterNamePropertyChanged();
                }
            }
        }

        #endregion
        #region WorkConditionId

        private System.Nullable<long> _WorkConditionId = null;
        partial void OnBeforeWorkConditionIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData);
        partial void OnBeforeWorkConditionIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData);
        partial void OnAfterWorkConditionIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<long> WorkConditionId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<long>>(_WorkConditionId);
                OnBeforeWorkConditionIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<long>>(value, _WorkConditionId);
                OnBeforeWorkConditionIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkConditionId", eventData.Value, true, true, true);
                    OnAfterWorkConditionIdPropertyChanged();
                }
            }
        }

        #endregion
        #region ServerConnectionId

        private System.Nullable<System.Int32> _ServerConnectionId = null;
        partial void OnBeforeServerConnectionIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<System.Int32>> eventData);
        partial void OnBeforeServerConnectionIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<System.Int32>> eventData);
        partial void OnAfterServerConnectionIdPropertyChanged();
        public System.Nullable<System.Int32> ServerConnectionId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<System.Int32>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<System.Int32>>(_ServerConnectionId);
                OnBeforeServerConnectionIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<System.Int32>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<System.Int32>>(value, _ServerConnectionId);
                OnBeforeServerConnectionIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ServerConnectionId", eventData.Value, true, true, true);
                    OnAfterServerConnectionIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemWorkConditions

        private WorkItemWorkConditionCollection _WorkItemWorkConditions;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public WorkItemWorkConditionCollection WorkItemWorkConditions
        {
            get
            {
                return _WorkItemWorkConditions;
            }
            internal set
            {
                LoadInstanceField("WorkItemWorkConditions", value, true, false, false);
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadWorkItemWorkConditions

        public void LoadWorkItemWorkConditions(WorkItemWorkConditionCollection value)
        {
            LoadInstanceField("WorkItemWorkConditions", value, true, true, false);
        }

        #endregion
        #region LoadWorkItemWorkConditionsAsync

        public async System.Threading.Tasks.Task<WorkItemWorkConditionCollection> LoadWorkItemWorkConditionsAsync()
        {
            return await LoadWorkItemWorkConditionsAsync(false, true, null, null, null);
        }
        public async System.Threading.Tasks.Task<WorkItemWorkConditionCollection> LoadWorkItemWorkConditionsAsync(bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemWorkConditionField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemWorkConditionRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkConditionManager.LoadWorkItemWorkConditionsAsync(this, refresh, raisePropertyChanged, criteria, strategyContext, objectParameters);

            return this.WorkItemWorkConditions;
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public WorkCondition Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public WorkCondition Copy()
        {
            return Copy(null);
        }
        public WorkCondition Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new WorkCondition();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.WorkItemManager.Data.IWorkCondition item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.WorkItemManager.Data.IWorkCondition item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(WorkCondition destination, global::Sidvall.WorkItemManager.Data.IWorkCondition source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkCondition destination, global::Sidvall.WorkItemManager.Data.IWorkCondition source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(WorkCondition destination, WorkCondition source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkCondition destination, WorkCondition source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.WorkItemManager.Data.IWorkCondition, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region SaveItemAsync

        public async System.Threading.Tasks.Task<WorkCondition> SaveItemAsync()
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkConditionManager.SaveItemAsync(this);
        }
        public async System.Threading.Tasks.Task<WorkCondition> SaveItemAsync(Sidvall.Data.SaveMode mode)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkConditionManager.SaveItemAsync(this, mode);
        }
        public async System.Threading.Tasks.Task<WorkCondition> SaveItemAsync(Sidvall.Data.SaveSettings settings)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkConditionManager.SaveItemAsync(this, settings);
        }
        public async System.Threading.Tasks.Task<WorkCondition> SaveItemAsync(Sidvall.Data.SaveSettings settings, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkConditionManager.SaveItemAsync(this, settings, strategyContext, objectParameters);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkConditionManager.Initialize(this, settings);
        }

        #endregion
        
        #region IWorkCondition Members

        string global::Sidvall.WorkItemManager.Data.IWorkCondition.Codename
        {
            get
            {
                return this._Codename;
            }
            set
            {
                this._Codename = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkCondition.ContextEditor
        {
            get
            {
                return this._ContextEditor;
            }
            set
            {
                this._ContextEditor = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkCondition.Description
        {
            get
            {
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkCondition.Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
        System.Nullable<long> global::Sidvall.WorkItemManager.Data.IWorkCondition.WorkConditionId
        {
            get
            {
                return this._WorkConditionId;
            }
            set
            {
                this._WorkConditionId = value;
            }
        }
        System.Nullable<System.Int32> global::Sidvall.WorkItemManager.Data.IWorkCondition.ServerConnectionId
        {
            get
            {
                return this._ServerConnectionId;
            }
            set
            {
                this._ServerConnectionId = value;
            }
        }
        System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> global::Sidvall.WorkItemManager.Data.IWorkCondition.WorkItemWorkConditions
        {
            get
            {
                return this._WorkItemWorkConditions;
            }
        }
        void global::Sidvall.WorkItemManager.Data.IWorkCondition.SetWorkItemWorkConditions(Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> items)
        {
            UpdateChildProperty(this._WorkItemWorkConditions, items);
            this._WorkItemWorkConditions = items as WorkItemWorkConditionCollection;
        }

        #endregion

        #endregion
        #region Constructors

        public WorkCondition()
        {
        }

        public WorkCondition(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}